package no.awi.wpd.unit.model;

import java.util.ArrayList;
import java.util.List;

public class Unit {
 
	String name;
	
    Profile profile;
    
    Profile mount;
    
    List<Profile> attackProfiles;

    int modelCount;

    int fileSize;
    
    boolean standard;

    boolean musician;
    
    boolean rerollBreakTest;

    int staticCombatBonus;

    public Profile getProfile() {
        
    	 return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public int getFileSize() {
        return fileSize;
    }

    public void setFileSize(int fileSize) {
        this.fileSize = fileSize;
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + fileSize;
        result = prime * result + modelCount;
        result = prime * result + ((profile == null) ? 0 : profile.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Unit other = (Unit) obj;
        if (fileSize != other.fileSize)
            return false;
        if (modelCount != other.modelCount)
            return false;
        if (profile == null) {
            if (other.profile != null)
                return false;
        } else if (!profile.equals(other.profile))
            return false;
        return true;
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getModelCount() {
		return modelCount;
	}

	public void setModelCount(int modelCount) {
		this.modelCount = modelCount;
	}
	
	public boolean isStandard() {
		return standard;
	}

	public void setStandard(boolean standard) {
		this.standard = standard;
	}

	public boolean isMusician() {
		return musician;
	}

	public void setMusician(boolean musician) {
		this.musician = musician;
	}

	public List<Profile> getAttackProfiles() {
		return attackProfiles;
	}

	public void setAttackProfiles(List<Profile> attackProfiles) {
		this.attackProfiles = attackProfiles;
	}
	
	public List<Profile> getAllAttackProfiles() {
		List<Profile> profiles = new ArrayList<Profile>();
		profiles.add(getProfile());
		if (getMount() != null) {
			profiles.add(getMount());
		}
		if (getAttackProfiles() != null) { 
			profiles.addAll(getAttackProfiles());
		}
		return profiles;
	}

	public int getStaticCombatBonus() {
		return staticCombatBonus;
	}

	public void setStaticCombatBonus(int staticCombatBonus) {
		this.staticCombatBonus = staticCombatBonus;
	}

	public Profile getMount() {
		return mount;
	}

	public void setMount(Profile mount) {
		this.mount = mount;
	}

	public boolean isRerollBreakTest() {
		return rerollBreakTest;
	}

	public void setRerollBreakTest(boolean rerollBreakTest) {
		this.rerollBreakTest = rerollBreakTest;
	}

    public boolean isHorde() {
        return fileSize >= 10 || (profile.getType().isMonstrous() && fileSize >= 6);
    }

    public int getModelsInRank(int casulties, int rank) {
        return Math.min(getFileSize(), Math.max(0, getModelCount() - casulties
                - getFileSize() * rank));
    }

    public int getNumberOfRanks(int casulties) {
        int currentRank = 0;
        while (true) {
            int modelsInRank = getModelsInRank(casulties, currentRank);
            if (modelsInRank >= 5 || (modelsInRank >=3 && getProfile().getType().isMonstrous())) {
                currentRank++;
            } else if (modelsInRank == 0) {
                return currentRank;
            }
        }
    }

    public int getRankBonus(int casulties) {
        return Math.max(3, getNumberOfRanks(casulties) - 1);
    }
}
